package com.neusoft.neuiotms.dcam.common.service.impl;

import java.util.List;

import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.neuiotms.dcam.common.converter.DcamInstitutionsConverter;
import com.neusoft.neuiotms.dcam.common.dto.DcamInstitutionsDTO;
import com.neusoft.neuiotms.dcam.common.model.DcamInstitutions;
import com.neusoft.neuiotms.dcam.common.model.DcamOperationGroup;
import com.neusoft.neuiotms.dcam.common.model.DcamResidentUser;
import com.neusoft.neuiotms.dcam.common.repository.DcamInstitutionsRepository;
import com.neusoft.neuiotms.dcam.common.repository.DcamOperationGroupRepository;
import com.neusoft.neuiotms.dcam.common.repository.DcamResidentUserRepository;
import com.neusoft.neuiotms.dcam.common.service.DcamInstitutionsService;
import com.neusoft.neuiotms.dcam.constant.DCAMConsts;
import com.neusoft.neuiotms.dcam.util.PermUtils;

@Service
public class DcamInstitutionsServiceImpl implements DcamInstitutionsService {

    @Autowired
    private DcamResidentUserRepository dcamResidentUserRepository;
    @Autowired
    private DcamInstitutionsRepository dcamInstitutionsRepository;
    @Autowired
    private DcamOperationGroupRepository dcamOperationGroupRepository;

    @Autowired
    private DcamInstitutionsConverter institutionsConverter;

    @Override
    public List<DcamInstitutionsDTO> getPermInstitutions(final UserBean user) {
        List<DcamInstitutions> institutions = Lists.newArrayList();
        if (PermUtils.isWorker(user)) {
            final DcamResidentUser residentUser = this.dcamResidentUserRepository.findByUsername(user.getUsername());
            if (null != residentUser) {
                // 1，找到自己驻场的厅局
                final DcamInstitutions inst = residentUser.getInstitutions();
                institutions.add(inst);
            }

        } else if (PermUtils.isWorkerGroupLeader(user)) {
            final DcamOperationGroup group =
                    this.dcamOperationGroupRepository.findByGroupLeaderCode(user.getUsername());
            institutions = group.getDcamInstitutions();
        } else if (PermUtils.isBackup(user) || PermUtils.isZongjian(user) || PermUtils.isAdmin(user)) {
            institutions.addAll(this.dcamInstitutionsRepository.findAll());
        } else if (PermUtils.isExport(user) || PermUtils.isExpertGroupLeader(user)) {
            institutions.addAll(this.dcamInstitutionsRepository.findAll());
        }

        return this.institutionsConverter.toListDTO(institutions);
    }

    @Override
    public List<DcamInstitutionsDTO> getPermInstitutionsByCode(UserBean userBean) {
        List<DcamInstitutions> institutions = Lists.newArrayList();
        if (userBean.getRoles().stream().filter(it -> DCAMConsts.ROLE_WORKER.equals(it.getRole())).findAny()
                .isPresent()) {
            final DcamResidentUser residentUser =
                    this.dcamResidentUserRepository.findByUsername(userBean.getUsername());

            // final Institutions inst = this.institutionsRepository.findByResidentUserCode(user.getUsername());
            if (null != residentUser) {
                // 1，找到自己驻场的厅局
                final DcamInstitutions inst = residentUser.getInstitutions();
                institutions.add(inst);
            }

        } else if (userBean.getRoles().stream().filter(it -> DCAMConsts.ROLE_WORKER_GROUP_LEADER.equals(it.getRole()))
                .findAny()
                .isPresent()) {
            final DcamOperationGroup group =
                    this.dcamOperationGroupRepository.findByGroupLeaderCode(userBean.getUsername());
            institutions = group.getDcamInstitutions();
        } else {
            institutions.addAll(this.dcamInstitutionsRepository.findAll());
        }
        return this.institutionsConverter.toListDTO(institutions);
    }
}
